package com.hospital.dao.impl;

import com.hospital.dao.DoctorDao;
import com.hospital.model.Doctor;
import com.hospital.util.DruidUtil;

import java.sql.*;

/**
 * 医生数据访问对象实现类
 * 实现对医生信息的数据库操作，包括查询、更新、添加等功能
 */
public class DoctorDaoImpl implements DoctorDao {
    
    /**
     * 根据医生ID查找医生信息
     * @param doctorId 医生ID
     * @return 医生对象，如果未找到则返回null
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public Doctor findByDoctorId(String doctorId) throws SQLException {
        String sql = "SELECT * FROM Doctor WHERE doctorId = ?";
        
        try (Connection conn = DruidUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, doctorId);
            ResultSet rs = pstmt.executeQuery();
            
            if (rs.next()) {
                Doctor doctor = new Doctor();
                doctor.setDoctorId(rs.getString("doctorId"));
                doctor.setDoctorName(rs.getString("doctorName"));
                doctor.setDoctorGender(rs.getString("doctorGender").charAt(0));
                doctor.setDoctorAge(rs.getInt("doctorAge"));
                doctor.setDoctorPassword(rs.getString("doctorPassword"));
                doctor.setDoctorIdCard(rs.getString("doctorIdCard"));
                doctor.setDoctorPhone(rs.getString("doctorPhone"));
                doctor.setDeptId(rs.getInt("deptId"));
                return doctor;
            }
            return null;
        }
    }

    /**
     * 更新医生信息（电话和密码）
     * @param doctorId 医生ID
     * @param newPhone 新电话号码
     * @param newPassword 新密码
     * @return 更新是否成功
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public boolean updateDoctorInfo(String doctorId, String newPhone, String newPassword) throws SQLException {
         String sql = "{CALL UpdateDoctorPhoneAndPassword(?, ?, ?)}";;
        
        try (Connection conn = DruidUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(2, newPhone);
            pstmt.setString(3, newPassword);
            pstmt.setString(1, doctorId);
            
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        }
    }
    /**
     * 检查医生ID是否已存在
     * @param doctorId 医生ID
     * @return 如果ID已存在返回true，否则返回false
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public boolean isDoctorIdExists(String doctorId) throws SQLException {
        String sql = "SELECT COUNT(*) FROM Doctor WHERE doctorId = ?";

        try (Connection conn = DruidUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, doctorId);
            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                return rs.getInt(1) > 0;
            }
            return false;
        }
    }
    /**
     * 添加新医生
     * @param doctor 医生对象，包含医生的所有信息
     * @return 添加是否成功
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public boolean addDoctor(Doctor doctor) throws SQLException {
        String sql = "INSERT INTO Doctor (doctorName, doctorGender, doctorAge, doctorIdCard, " +
                     "doctorPhone, deptId, doctorPassword,doctorId) VALUES (?, ?, ?, ?, ?, ?, ?,?)";
        
        try (Connection conn = DruidUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, doctor.getDoctorName());
            pstmt.setString(2, String.valueOf(doctor.getDoctorGender()));
            pstmt.setInt(3, doctor.getDoctorAge());
            pstmt.setString(4, doctor.getDoctorIdCard());
            pstmt.setString(5, doctor.getDoctorPhone());
            pstmt.setInt(6, doctor.getDeptId());
            // 设置默认密码为身份证后六位
            String defaultPassword = doctor.getDoctorIdCard().substring(doctor.getDoctorIdCard().length() - 6);
            pstmt.setString(7, defaultPassword);
            pstmt.setString(8, doctor.getDoctorId());
            return pstmt.executeUpdate() > 0;
        }
    }
} 